import threading
from datetime import datetime
import uuid
from threading import Timer

__timeid = dict()


def setTimeout(fun, s=0):
    s = s / 1000
    timeout = threading.Timer(s, function=fun)
    timeout.start()
    key = str(uuid.uuid4()) + str(datetime.now().timestamp())
    __timeid.setdefault(key, timeout)
    return key


def setInterval(fun, s=0):
    key = str(uuid.uuid4()) + str(datetime.now().timestamp())
    s = s / 1000

    def run():
        fun()
        time_out: Timer = threading.Timer(s, function=run)
        time_out.start()
        __timeid.setdefault(key, time_out)

    timeout = threading.Timer(s, function=run)
    timeout.start()
    __timeid.setdefault(key, timeout)
    return key


def clearTimeout(id):
    timeout: threading.Timer = __timeid.get(id)
    timeout.cancel()


def clearInterval(id):
    clearTimeout(id)
